<html>
<head>
<script type="text/javascript" src="js/namespace.js"></script>
<script type="text/javascript" src="js/value_manipulation.js"></script>
<script type="text/javascript" src="js/user_data.js"></script>
<script type="text/javascript" src="js/urls.js"></script>
<script type="text/javascript">

chrome.extension.onConnect.addListener(function(port) {
   port.onMessage.addListener(function(msg) {
      switch( msg.action )
      {
         case "receive":
            fetchDocumentPosition( msg, port );
            break;
         case "send":
            saveDocumentPosition( msg, port );
            break;
         case "store_userdata":
            setUserData( msg );
            break;
         case "log":
            alert( msg.message );
            break;
      }
   });
});

function fetchDocumentPosition( obj, port )
{
   var url = getFullFetchURL( obj.href );
   var xhr = getRequest( url );
   xhr.onreadystatechange = function() {
     // There is a lot of logic here to expedite the process.
     // this method splits the data into several smaller parts 
     // before sending it to the dependant methods.  It is not
     // ideal, but it prevents some rather long delays...
     if (xhr.readyState == 4) {
         try
         {
            var response  = JSON.parse( xhr.responseText );
            var hw = {};
            if( response.height > 0 ) hw.h  = response.height;
            if( response.width  > 0 ) hw.w  = response.width;
            resizeWindow( hw );
     
            var sp = {};
            sp.scrollTop = response.scroll_top;
            sp.href      = obj.href;
            port.postMessage( sp );
        }
        catch( e )
        {
           alert( e );
        }
     }
   }
   xhr.send();
}

function saveDocumentPosition( obj, port )
{
   try
   {
      addUserData( "position",  obj.scrollTop );
      var url = obj.topURL;
      url = getGoogleDocsURL( url );
      addUserData( "root_hash", getHash( url ) );
      url = getFullSaveURL( obj.href );
      var xhr = getRequest( url );
      xhr.onreadystatechange = function() {
         try
         {
            var json = JSON.parse( xhr.responseText );
            if( castToBoolean( json.serverMessage ) ) alert( json.serverMessage );
            if( xhr.readyState == 4 && xhr.status == 200 && xhr.responseText && xhr.responseText.length && castToBoolean( json.success ) )
            {
               alert( "Your data has been successfully saved." );
            }
            else if( xhr.readyState == 4 )
            {
               //TODO: There really should be some message config file for this.
               alert( "For some reason we were unable to save your data, please try again.  Message = " + xhr.responseText );
            }   
         }
         catch( e )
         {
            alert( "There was an error processing your request: " + e + " " + xhr.responseText );
         }
      }
      xhr.send();
   }
   catch( e )
   {
      alert( e );
   }
}


</script>
</head>
   <body>
   </body>
</html>
